'use strict';
var pg = require('../lib/pgClient');
const log = require('log4js').getLogger("models/type");
const async = require('async');
let sqlStart = "START TRANSACTION;";
let sqlRollback = "ROLLBACK;";
let sqlCommit = "COMMIT;";
/**
 * 添加分类
 */
const add_sql = 'INSERT INTO type (typename, status, lcon, createtime) VALUES ($1, $2, $3, $4)';//添加的sql语句
exports.add_type = function (args, params, callback){
    pg.query(add_sql, params, function(err, result){
        if(err){
            callback (err,null);
        }else {
            callback(err,result);
        }
    })
};

/**
 *批量删除分类信息
 */
exports.remove_type = function(args, callback){
    let ids = args.ids;//ids 是数组
    let pids = [];
    let theids = "'"+ids.join("','")+"'";//将数组ids整理成字符串
    const remove_sql1 = 'DELETE FROM type WHERE id IN ('+theids+')';//删除id在theids的分类数据
    const remove_sql2 = 'SELECT id FROM type WHERE id IN ('+theids+')';//查询id在theids的分类数据
    const remove_sql5 = 'DELETE FROM picture WHERE tid IN ('+theids+')';//删除分类id在theids的图片数据
    const remove_sql6 = 'SELECT id FROM picture WHERE tid IN ('+theids+')';//查询tid在theids的图片数据
    pg.query(remove_sql2, [], function(err,result){//执行查询操作
        if(err){
            callback (err,null);
        }else if(result.rowCount == 0) {//分类不存在
            callback (err,null);
        }else{
            pg.connect(function (err, client, done) {
                client.query(sqlStart, function () {
                    client.query(remove_sql1, [], function (err, result) {//查作者
                        if (err) {
                            log.error(err);
                            client.query(sqlRollback, function (errRollback) {
                                if (errRollback) {
                                    log.error('删除分类报错');
                                }
                                done();
                                callback(err, null);
                            })
                        } else if (result.rowCount == 0) {
                            log.error('删除分类失败');
                            client.query(sqlRollback, function (errRollback) {
                                if (errRollback) {
                                    log.error('删除分类失败');
                                }
                                done();
                                return callback(err,'null');
                            })
                        } else {
                            client.query(remove_sql6, [], function (err, result) {//查分类
                                if (err) {
                                    log.error(err);
                                    client.query(sqlRollback, function (errRollback) {
                                        if (errRollback) {
                                            log.error('删除关联记录失败');
                                        }
                                        done();
                                        callback(err, null);
                                    })
                                } else {
                                    log.debug(result);
                                    if(result.rowCount==0||result.rows.length==0){
                                        pids.push(0);
                                    }else {
                                       let rows = result.rows;
                                       rows.forEach(function(row){
                                           pids.push(row.id);
                                       })
                                    }
                                    let thepids = "'"+pids.join("','")+"'";//将数组pids整理成字符串
                                    const remove_sql3 = 'DELETE FROM projestlogs WHERE pictureid IN ('+thepids+')';//查询id在theids的分类图片关联数据
                                    const remove_sql4 = 'DELETE FROM brandlogs WHERE pictureid IN ('+thepids+')';//查询id在theids的分类品牌关联数据
                                    const remove_sql7 = 'DELETE FROM wantlogs WHERE pid IN ('+thepids+')';//查询id在theids的分类图片想要数据
                                    const remove_sql6 = 'DELETE FROM ownlogs WHERE pid IN ('+thepids+')';//查询id在theids的分类图片拥有数据
                                    client.query(remove_sql5, [], function (err, result) {//添加图片
                                        if (err) {//失败
                                            log.error(err);
                                            client.query(sqlRollback, function (errRollback) {
                                                if (errRollback) {
                                                    log.error('删除分类品牌关联失败');
                                                }
                                                done();
                                                callback(err,null);
                                            });
                                        } else {
                                            client.query(remove_sql3, [], function (err, result) {//查分类
                                                if (err) {
                                                    log.error(err);
                                                    client.query(sqlRollback, function (errRollback) {
                                                        if (errRollback) {
                                                            log.error('删除图片项目关联记录失败');
                                                        }
                                                        done();
                                                        callback(err, null);
                                                    })
                                                } else {
                                                    client.query(remove_sql4, [], function (err, result) {//添加图片
                                                        if (err) {//失败
                                                            log.error(err);
                                                            client.query(sqlRollback, function (errRollback) {
                                                                if (errRollback) {
                                                                    log.error('删除图片品牌关联失败');
                                                                }
                                                                done();
                                                                callback(err,null);
                                                            });
                                                        } else {
                                                            client.query(remove_sql6, [], function (err, result) {//查分类
                                                                if (err) {
                                                                    log.error(err);
                                                                    client.query(sqlRollback, function (errRollback) {
                                                                        if (errRollback) {
                                                                            log.error('删除分类图片想要数据记录失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    })
                                                                } else {
                                                                    client.query(remove_sql7, [], function (err, result) {//添加图片
                                                                        if (err) {//失败
                                                                            log.error(err);
                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                if (errRollback) {
                                                                                    log.error('删除分类图片拥有数据失败');
                                                                                }
                                                                                done();
                                                                                callback(err,null);
                                                                            });
                                                                        } else {
                                                                            client.query(sqlCommit, function (errCommit) {
                                                                                if (errCommit) {
                                                                                    client.query(sqlRollback, function (errRollback) {
                                                                                        if (errRollback) {
                                                                                            log.error('回滚失败');
                                                                                        }
                                                                                        done();
                                                                                        callback(err, null);
                                                                                    });
                                                                                } else {
                                                                                    done();
                                                                                    return callback(err, result);//删除成功
                                                                                }
                                                                            });
                                                                        }
                                                                    });
                                                                }
                                                            })
                                                        }
                                                    });
                                                }
                                            })

                                        }
                                    });
                                }
                            })

                        }
                    });
                });
            })
        };
    });

};
/**
 * 批量编辑分类信息状态
 */
exports.update_status = function(args, callback){
    let ids = args.ids;//ids 是数组
    let theids = "'"+ids.join("','")+"'";//将数组ids整理成字符串
    const upd_sql = 'UPDATE type SET status = $1 WHERE id IN ('+theids+')';//编辑id在theids的分类数据
    const params =[
        args.status
    ]
    pg.query(upd_sql, params, function(err,result){//执行操作
        if(err){//报错
            callback (err,null);
        }else {
            callback (err,result);//执行结束
        }
    });
};
/**
 * 分类列表
 * @param params条件
 * @param callback
 */
const list_sql = 'SELECT * FROM type WHERE ORDER BY createtime DESC LIMIT $1 OFFSET $2';//根据创建时间倒序排列加分页
exports.list = function(params, callback){
    pg.query(list_sql, params, function(err,result){//params主要是含有分页信息
        if(err){//报错
            callback (err, null);
        }else {//有返回信息
            callback (err, result);
        }
    });
};
/**
 * 分类列表
 * @param params条件
 * @param callback
 */
const list_app_sql = 'SELECT * FROM type WHERE status = $1 ORDER BY createtime DESC';//根据创建时间倒序排列加分页
exports.list_app = function( callback){
    pg.query(list_app_sql, [1], function(err,result){//params主要是含有分页信息
        if(err){//报错
            callback (err, null);
        }else {//有返回信息
            callback (err, result);
        }
    });
};
/**
 * 后台列表数据统计
 * @param params
 * @param callback
 */
const count_sql = 'SELECT count(*) FROM type ';//统计总数据
exports.count_b = function(params, callback){//怕rams=[]
    pg.query(count_sql, params, function(err,result){
        if(err){//报错
            callback (err, null);
        }else {
            callback (err, result.rows[0]);//找到对应的返回信息中的有效数据
        }
    })
}